﻿using MyToolkit.Multimedia;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Windows.Web.Syndication;
using YoutubeApp.Model;

namespace YoutubeApp.Services
{
    public class YoutubeServices : IYoutubeServices
    {
        public YoutubeServices()
        {

        }
        /// <summary>
        /// get Data Source and set source for MediaPlayer
        /// </summary>
        /// <param name="youTubeId"></param>
        /// <param name="maxQuality"></param>
        /// <param name="videoData"></param>
        /// <returns></returns>
        public async Task<YouTubeUri> GetVideoUriAsync(string youTubeId, YouTubeQuality maxQuality)
        {
            YouTubeUri urlVideo = null;
            try
            {
                if (NetworkInterface.GetIsNetworkAvailable())
                {
                    string videoId = youTubeId;
                    if (!videoId.Equals(String.Empty))
                    {
                        //Get The Video Uri and set it as a player source
                        var url = await YouTube.GetVideoUriAsync(videoId, YouTubeQuality.Quality480P);
                        if (url != null)
                            urlVideo = url;
                    }

                }
                else
                {
                    MessageDialog message = new MessageDialog("You're not connected to Internet!");
                    await message.ShowAsync();
                }
            }
            catch
            {
            }
            return urlVideo;
        }
        /// <summary>
        /// Get Youtube Channel
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public async Task<List<YoutubeVideo>> GetYoutubeChannel(string url)
        {
            try
            {
                SyndicationClient client = new SyndicationClient();
                SyndicationFeed feed = await client.RetrieveFeedAsync(new Uri(url));

                List<YoutubeVideo> videosList = new List<YoutubeVideo>();
                YoutubeVideo video;
                foreach (SyndicationItem item in feed.Items)
                {
                    video = new YoutubeVideo();
                    video.YoutubeLink = item.Links[0].Uri;
                    string a = video.YoutubeLink.ToString().Remove(0, 31);
                    video.Id = a.Substring(0, 11);
                    video.Title = item.Title.Text;
                    video.PubDate = item.PublishedDate.DateTime;
                    video.Thumbnail = YouTube.GetThumbnailUri(video.Id, YouTubeThumbnailSize.Small);
                    videosList.Add(video);
                }

                return videosList;
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        ///Get Youtube Playlist 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public async Task<List<YoutubeVideo>> GetYoutubePlaylist(string url)
        {
            try
            {
                SyndicationClient client = new SyndicationClient();
                SyndicationFeed feed = await client.RetrieveFeedAsync(new Uri(url));

                List<YoutubeVideo> videosList = new List<YoutubeVideo>();
                YoutubeVideo video;
                foreach (SyndicationItem item in feed.Items)
                {
                    video = new YoutubeVideo();

                    video.YoutubeLink = item.Links[0].Uri;
                    string a = video.YoutubeLink.ToString().Remove(0, 31);
                    video.Id = a.Substring(0, 11);
                    video.Title = item.Title.Text;
                    video.PubDate = item.PublishedDate.DateTime;

                    video.Thumbnail = YouTube.GetThumbnailUri(video.Id, YouTubeThumbnailSize.Small);

                    videosList.Add(video);
                }
                return videosList;
            }
            catch
            {
                return null;
            }
        }
    }
}
